#! /bin/sh
# PCP QA Test No. 541
# another variant on ...
# #535080 - dynamic indoms for pmie, uses sample.dynamic.*
# this time, the problem is when the intial instance domain contains
# one instance, the pmie expression tree uses the 1:1 operator instead
# of the n:1 operator
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!
control=$PCP_PMDAS_DIR/sample/dynamic.indom

_cleanup()
{
    [ -f $control.$seq ] && $sudo mv $control.$seq $control
    rm -f $tmp.*
}

$sudo rm -f $control.$seq

trap "_cleanup; exit \$status" 0 1 2 3 15

# real QA test starts here

[ -f $control ] && $sudo mv $control $control.$seq

if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1

# prime the sample PMDA to refresh the dynamic indom
#
pminfo -f sample.dynamic >/dev/null 2>&1

# just one instance
#
cat >$tmp.indom <<End-of-File
0 zero
End-of-File
$sudo rm -f $control
$sudo cp $tmp.indom $control
pmprobe -I sample.dynamic.counter

cat <<End-of-File | pmie -t 500msec >$tmp.out 2>&1 &
some_inst ( sample.dynamic.counter > 0 ) -> shell "echo some_inst counter:" " %i:?";
all_inst ( sample.dynamic.counter > 0 ) -> shell "echo all_inst counter:" " %i:?";
End-of-File
pid=$!

sleep 3

# replace one instance by two others
#
cat >$tmp.indom <<End-of-File
1 one
2 two
End-of-File
$sudo rm -f $control
$sudo cp $tmp.indom $control
pmprobe -I sample.dynamic.counter

sleep 3

$signal -s TERM $pid
wait

echo
echo "filtered pmie output ..."
cat $tmp.out >>$seq_full
# Especially on slow VMs (like bozo-vm) we may need to dodge warnings
# from pmie's rule scheduler around pmcd reconnection ... the second
# awk script does this.
#
$PCP_AWK_PROG <$tmp.out '
	{ if (seen[$0] == 1) next
	  print
	  seen[$0] = 1
	}' \
| $PCP_AWK_PROG '
skip == 1 && /^[^ ]/		{ skip = 0 }
/^run: schedule eval/		{ skip = 1 }
/^sleepTight: negative/		{ skip = 1 }
/^Last sleepTight until:/	{ skip = 1 }
/^This sleepTight\() entry:/	{ skip = 1 }
/^Harvest children done:/	{ skip = 1 }
/^Want sleepTight until:/	{ skip = 1 }
/Task dump @/			{ skip = 1 }
skip == 0			{ print }'

# success, all done
status=0
exit
